#!/bin/bash
if [ ! $1 ]
then
	echo "ERROR! Need input file!"
fi

sed -n '/<Place/,/<\/Placemark>/p' $1 | \
sed	-n \
	-e "/<Placemark>/p" \
	-e "/<\/Placemark>/p" \
	-e "s/\(<name>\)\(.*\)\(<\/name>\)/\'name\':\'\2\',/p" \
	-e "s/<coordinates>\(.*\),\(.*\),.*<\/coordinates>/\'ll\':{\'tp\':\'1\',\'lat\':\'\2\',\'lng\':\'\1\'}/p" \
	-e "s/地址[:： ]\{,100\}\([^<]*\)/[\'adr\':\'\1\']/" \
	-e "s/电话[:： ]\{,100\}\([^<]*\)/[\'tel\':\'\1\']/" \
	-e "s/详情[:： ]\{,100\}\([^<]*\)/[\'desc\':\'\1\']/" \
	-e "s/网址[:： ]\{,100\}\([^<]*\)/[\'url\':\'\1\']/" \
	-e "s/\][^\[]*\[/,/g" \
	-e "/<description>/s/^[^\']*//" \
	-e "/<\/description>/s/].*$/,/p" | \
sed 	-nr "H; $ {x;s/\n//g;p};" | \
sed 	-nr "H; $ {x;s/ //g;p};" | \
sed	-e "s/<Placemark>'name':'\([^\']*\)'/\'\1\':{\'name\':\'\1\'/g" \
	-e "s/<\/Placemark>/},/g" | \
sed "/^$/d" | \
sed "s/,$//"	> tmp/file1

city="北京市"
echo $city

if [ -f beijing.json ]
then
	rm beijing.json
fi

printf "\'$city\':{\n'detail':{\n" >> beijing.json 
cat tmp/file1 >> beijing.json
printf "}}" >> beijing.json
